package com.zsr.example.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zsr.example.config.annotation.OptLog;
import com.zsr.example.config.annotation.RequiresPermissions;
import com.zsr.example.entity.SysDict;
import com.zsr.example.entity.SysDictItem;
import com.zsr.example.service.SysDictItemService;
import com.zsr.example.service.SysDictService;
import com.zsr.example.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zsr.example.util.constants.SysLogConstants.UPDATE;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsr
 * @since 2022-01-04
 */
@RestController
@RequestMapping("/sys-dict")
public class SysDictController {
    @Autowired
    private SysDictService dictService;
    @Autowired
    private SysDictItemService dictItemService;

    @RequiresPermissions("dict:list")
    @GetMapping("getDictList")
    public JSONObject getDictList(){
        List<SysDict> list = dictService.list(null);
        return CommonUtil.successJson(list);
    }
    @RequiresPermissions("dict:list")
    @GetMapping("getDictItemListByCode/{key}")
    public JSONObject getDictItemListByCode(@PathVariable String key){
        List<SysDictItem> list = dictItemService.list(new LambdaQueryWrapper<SysDictItem>()
        .eq(SysDictItem::getDictCode,key));
        return CommonUtil.successJson(list);
    }
    @OptLog(optType = UPDATE,optDesc = "修改系统配置")
    @RequiresPermissions("dict:update")
    @PostMapping("updateDictItem/{key}")
    public JSONObject updateDictItem(@PathVariable String key,@RequestBody List<SysDictItem> list) {
        dictItemService.updateBatchById(list);
        return CommonUtil.success();
    }

}

